package com.dmap.analysispoint.controller.rpc;

import com.dmap.analysispoint.constants.BurialPointConstant;
import com.dmap.analysispoint.service.DataPreparationService;
import com.dmap.base.constants.Constant;
import com.dmap.base.send.email.MailService;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;

/**
 * \* @author:zhouhao
 * \* Date:23/06/2018
 * \* Description:数据预处理
 * \*
 */
@Api(value = "数据预处理", description = "background/数据预处理")
@Controller
@RequestMapping(value = "/background/dmap-analysis-point-server/v2/")
public class DataPreparationController {


    @Value("${spring.profiles.active}")
    private String environment;

    @Resource
    private DataPreparationService dataPreparationService;

    private static Logger logger = LoggerFactory.getLogger(DataPreparationController.class);

    @ResponseBody
    @ApiOperation(value = "数据看板模块用户、设备数据分析",
            notes = "1.传入所要查询的参数，参数为查询的开始时间、查询的结束时间，其中查询的开始时间、查询的结束时间分别为当天零点时间戳、当前查询时间的时间戳\n" +
                    "2.返回处理记录条数")
    @RequestMapping(value = "countPhoneEquipmentNumAndAddCache", method = RequestMethod.GET)
    public BaseVo countPhoneEquipmentNumAndAddCache(@RequestParam(value = "startTime") Long startTime,
                                                    @RequestParam(value = "endTime") Long endTime) {
        BaseVo baseVo = new BaseVo();
        int success = 0;
        try {
            if (startTime == null || endTime == null || endTime == 0) {
                baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
                baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            }
            success = dataPreparationService.countPhoneEquipmentNumAndAddCache(startTime, endTime);
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对数据看板信息定时自动分析预处理定时任务 对数据看板手机、设备数据预处理定时任务成功");
            } catch (MessagingException me) {
                logger.error("按小时对数据看板信息定时自动分析预处理定时任务 对数据看板手机、设备数据预处理定时任务 MailService.sendMail error:  {}", me);
            }
        } catch (Exception e) {
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对数据看板信息定时自动分析预处理定时任务 对数据看板手机、设备数据预处理定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按小时对数据看板信息定时自动分析预处理定时任务 对数据看板手机、设备数据预处理定时任务 MailService.sendMail error:  {}", me);
            }
        }
        baseVo.setData(success);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "数据看板模块折线图数据预处理",
            notes = "1.传入所要查询的参数，参数为查询的开始时间、查询的结束时间，其中查询的开始时间、查询的结束时间分别为当天零点时间戳、当前查询时间的时间戳\n" +
                    "2.返回处理记录条数")
    @RequestMapping(value = "dataBoardAnalysis", method = RequestMethod.GET)
    public BaseVo dataBoardAnalysis(@RequestParam(value = "startTime") Long startTime,
                                    @RequestParam(value = "endTime") Long endTime) {
        BaseVo baseVo = new BaseVo();
        int success = 0;
        try {
            if (startTime == null || endTime == null || endTime == 0) {
                baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
                baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            }
            success = dataPreparationService.dataBoardAnalysis(startTime, endTime);
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按天对数据看板模块折线图数据定时自动分析预处理定时任务成功");
            } catch (MessagingException me) {
                logger.error("按天对数据看板模块折线图数据定时自动分析预处理定时任务 MailService.sendMail error:  {}", me);
            }
        } catch (Exception e) {
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按天对数据看板模块折线图数据定时自动分析预处理定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按天对数据看板模块折线图数据定时自动分析预处理定时任务 MailService.sendMail error:  {}", me);
            }
        }
        baseVo.setData(success);
        return baseVo;
    }
}
